package frame;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import bean.BusType;
import bean.Driver;
import dao.BusStopDao;
import dao.BusStopDaoImpl;


import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;

public class SeekWays {

    private JFrame frame;
    private JTable table;

    BusStopDao bsd=new BusStopDaoImpl();
    private JTextField textField;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SeekWays window = new SeekWays();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public SeekWays() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 771, 601);
        frame.setLocationRelativeTo(null); // Ĭ Ͼ     ʾ
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        frame.setVisible(true);

        //      м            Զ
        MYPanel2 bg = new MYPanel2();
        bg.setBounds(0, 0, 753, 528);
        //           ӵ frame  Ĭ ϴ
        frame.getContentPane().add(bg);
        //     bg Ĳ   Ϊ   Բ
        bg.setLayout(null);

        table = new JTable();
        tableInit(table);
        table.setBounds(14, 48, 725, 363);


        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setBounds(14, 48, 725, 363);
        bg.add(scrollPane);
        //  ʾ
        TableModel dataModel =getDataModel();
        table.setModel(dataModel);

        JButton button = new JButton("\u6DFB\u52A0\u4FE1\u606F");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                BusInformation bi=new BusInformation();
            }
        });
        button.setBounds(24, 426, 113, 27);
        bg.add(button);

        JButton button_1 = new JButton("\u4FEE\u6539\u4FE1\u606F");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int frow = table.getSelectedRow();	//  õ һ  ѡ   е

                if(frow == -1) {
                    JOptionPane.showMessageDialog(frame, "   ȵ   ѡ  һ    ¼");

                }else {
                    //String[] д     ѡ   е
                    String[] sendData = new String[table.getColumnCount()];	//ȡ ñ
                    for(int i=0; i<sendData.length; i++) {
                        sendData[i] = table.getValueAt(frow, i)+"";
                    }
                    //   ޸ ҳ 棬        Ϣ
                    BusInforChanged bic=new BusInforChanged(sendData);
                }
            }
        });
        button_1.setBounds(153, 426, 113, 27);
        bg.add(button_1);

        JButton button_2 = new JButton("\u5220\u9664\u4FE1\u606F");
        button_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int frow = table.getSelectedRow();	//  õ һ  ѡ   е
                if(frow == -1) {
                    JOptionPane.showMessageDialog(frame, "   ȵ   ѡ  һ    ¼");
                }else {
                    int b = JOptionPane.showConfirmDialog(frame, "ȷ  ɾ    ");
                    if(b == 0) {
                        if(frow == -1) {
                            JOptionPane.showMessageDialog(frame, "   ȵ   ѡ  һ    ¼");

                        }else {
                            String bid= table.getValueAt(frow, 0)+"";
                            bsd.deleteBus(bid);
                            JOptionPane.showMessageDialog(frame, "ɾ   ɹ   ");
                            //   ±
                            DefaultTableModel dtm = (DefaultTableModel) table.getModel();
                            dtm.removeRow(frow);
                            table.setModel(dtm);
                        }
                    }
                }
            }
        });
        button_2.setBounds(280, 426, 113, 27);
        bg.add(button_2);

        textField = new JTextField();
        textField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                //          ֵ  գ   ʾȫ
                String keyWord = textField.getText();
                String bySth = "bid";

                TableModel dataModel = getDataModel(bySth, keyWord);
                table.setModel(dataModel);	//
            }
            //   ϱ            û  Ӧ  Ӣ ĺ
            //   ±             з Ӧ  Ӣ Ĳ ̫  ʹ
            public void keyTyped(KeyEvent e) {
                String keyWord = textField.getText();
                String bySth = "bid";

                TableModel dataModel = getDataModel(bySth, keyWord);
                table.setModel(dataModel);	//
            }
        });
        textField.setBounds(425, 427, 86, 24);
        bg.add(textField);
        textField.setColumns(10);

        JLabel lblNewLabel = new JLabel("");
        lblNewLabel.setIcon(new ImageIcon(SeekWays.class.getResource("/images/\u516C\u4EA4\u8F66.png")));
        lblNewLabel.setBounds(462, -18, 171, 81);
        bg.add(lblNewLabel);

        JLabel label = new JLabel("\u516C\u4EA4\u8F66\u4FE1\u606F\u67E5\u8BE2");
        label.setFont(new Font("    ", Font.PLAIN, 30));
        label.setBounds(249, 0, 210, 48);
        bg.add(label);



        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);

        JMenu menu = new JMenu("\u5F00\u59CB");
        menuBar.add(menu);

        JMenu menu_2 = new JMenu("\u6B22\u8FCE");
        menu.add(menu_2);

        JMenu menu_3 = new JMenu("\u8FD4\u56DE");
        menu.add(menu_3);

        JMenuItem mntmNewMenuItem = new JMenuItem("\u8FD4\u56DE\u7528\u6237\u754C\u9762");
        menu_3.add(mntmNewMenuItem);
        mntmNewMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                UsersEnroll ue=new UsersEnroll();
                frame.setVisible(false);
            }
        });
        JMenuItem menuItem_1 = new JMenuItem("\u8FD4\u56DE\u7BA1\u7406\u5458\u754C\u9762");
        menu_3.add(menuItem_1);
        menuItem_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ManagerEnroll me=new ManagerEnroll();
                frame.setVisible(false);
            }
        });

        JMenu menu_1 = new JMenu("\u5E2E\u52A9");
        menuBar.add(menu_1);

        JMenuItem menuItem = new JMenuItem("\u6B22\u8FCE");
        menu_1.add(menuItem);
    }
    private void tableInit(JTable tb) {

        //  Ԫ
        DefaultTableCellRenderer cr = new DefaultTableCellRenderer();
        cr.setHorizontalAlignment(JLabel.CENTER);
        tb.setDefaultRenderer(Object.class, cr);

        //     и
        tb.setRowHeight(60);

        //         С
        tb.setFont(new Font("΢   ź ", 0, 20));
        tb.getTableHeader().setFont(new Font("΢   ź ", 0, 20));



    }
    protected TableModel getDataModel() {

        //         ֶ         ͷ
        String[] columnNames = bsd.getColumnNames("businfortable");

        //   ò ѯ     list б  ת  Ϊ  ά   飬
        List<BusType> buslist = bsd.getAllbuses();
        int row = buslist.size(); //               ݸ
        int column = columnNames.length; //             ֶθ

        Object[][] data = new Object[row][column];
        for (int i = 0; i < buslist.size(); i++) {
            data[i][0] = buslist.get(i).getBid();
            data[i][1] = buslist.get(i).getDid();
            data[i][2] = buslist.get(i).getBnum();
            data[i][3] = buslist.get(i).getBtype();
            data[i][4] = buslist.get(i).getBusstop();
        }

        TableModel dataModel = new DefaultTableModel(data, columnNames); //            ģ

        return dataModel;
    }
    protected TableModel getDataModel(String bySth, String keyWord) {

        //         ֶ         ͷ
        String[] columnNames = bsd.getColumnNames("businfortable");

        //   ò ѯ     list б  ת  Ϊ  ά   飬
        List<BusType> buslist = bsd.getBusbySth(bySth, keyWord);
        int row = buslist.size(); //               ݸ
        int column = columnNames.length; //             ֶθ

        Object[][] data = new Object[row][column];
        for (int i = 0; i < buslist.size(); i++) {
            data[i][0] = buslist.get(i).getBid();
            data[i][1] = buslist.get(i).getDid();
            data[i][2] = buslist.get(i).getBnum();
            data[i][3] = buslist.get(i).getBtype();
            data[i][4] = buslist.get(i).getBusstop();
        }

        TableModel dataModel = new DefaultTableModel(data, columnNames); //            ģ

        return dataModel;
    }
}

//    ͼ
class MYPanel2 extends JPanel {
    private Image img;

    // ʵ ֹ  췽
    public MYPanel2() {
        // TODO Auto-generated constructor stub
        //  ڹ  췽   ж   Ҫ  ͼƬ  Դ   г ʼ
        ImageIcon icon = new ImageIcon(UsersLoginFrame.class.getResource("/images/    ͼ2.jpg"));
        //   icon    ת    img   ͻ
        img = icon.getImage();
    }

    // JPanel     ṩ  һ        ʵ ֡           Ĺ
    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), null);
    }

}